/*
 * Copyright 2011 Alex Paréj.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mkdir.util;

/**
 *
 * @author Alex Paréj
 */
public final class FajlUtil {

    private final static String KITERJESZTES_ELVALASZTO = ".";
    private final static String B = "B";
    private final static String KB = "KB";
    private final static String MB = "MB";

    private FajlUtil() {
    }

    public static String getKiterjesztes(String fajlnev) {
        if (!StringUtil.isUres(fajlnev) && !fajlnev.endsWith(KITERJESZTES_ELVALASZTO)
                && fajlnev.length() > 1) {
            int hossz = fajlnev.length();
            int index = fajlnev.lastIndexOf(KITERJESZTES_ELVALASZTO);
            if (index > 0) {
                return fajlnev.substring(index + 1, hossz);
            }
        }
        return "";
    }

    public static String getMeret(byte[] fajl) {
        int meret = fajl != null ? fajl.length : 0;
        return getMeret(meret);
    }

    public static String getMeret(long meretBajtban) {

        if (meretBajtban < 1024) {
            return meretBajtban + B;
        }
        if (meretBajtban < 1024 * 1024) {
            return kerekit((float) meretBajtban / 1024) + KB;
        }

        return kerekit((float) meretBajtban / (1024 * 1024)) + MB;
    }

    private static int kerekit(float meret) {
        return Math.round(meret);
    }
}
